<?php

require_once('lib/utils.php');
require_once('lib/photo.php');

$dirs = photo_gallery_list();

if (count($dirs) > 0) {
	if (isset($_POST['select-gallery'])) {
		$gallery = clean_str($_POST['name']);
		if (photo_gallery_exists($gallery)) {
			$selected = $gallery;
		}
	}
	else {
		foreach ($dirs as $dir); // force last item
		$selected = basename($dir);
	}
	?>

	<h2>Galerie photo : <?php echo $selected ?></h2>
	<div id="galerie_container">

	<form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
		<p><big><strong>Sélectionnez une galerie :&nbsp;</strong></big>
		<select name="name">
		<?php
			foreach ($dirs as $dir) {
				$dir = basename($dir);
				echo '<option value="'.$dir.'"';
				if ($selected == $dir) {
					echo ' selected="selected"';
				}
				echo '>'.$dir.'&nbsp;&nbsp;</option>';
			}
		?>
		</select>&nbsp;
		<input type="submit" value="Voir" name="select-gallery" /></p>
	</form>

	<?php
	// affichage de la galerie
	$pictures = photo_list($selected);

	if (count($pictures) > 0) {
		?>
		<div>
			<?php
			foreach ($pictures as $pic) {
				$name = basename($pic);
				?>
				<div class="picture-item">
					<?php echo '
					<a href="media-photos_view-'.$name.'-'.$selected.'.html" title="'.$name.'">
						<img src="'.$pic.'" alt="'.$name.'" />
					</a>';
					?>
				</div>
			<?php } ?>
		</div>

		<?php
	}
	else {
		?>
		<p class="info">
			Il n'y a pas de photo dans cette galerie pour le moment
		</p>
		<?php
	}
	?>
	</div> <!-- end galerie_container -->
	<?php
}
else {
	?>
	<p class="info">
		Il n'y a pas de galerie photo pour le moment
	</p>
	<?php
}
?>
